package com.handler.http.message.handler;

import com.handler.http.message.constants.Constant;
import com.handler.http.message.response.ResultWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author guoQ.z
 * @date 2021/1/12 14:00
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class ServerExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultWrapper exceptionHandler(Exception ex) {
        System.out.println(ex);
        if (ex instanceof ServerException) {
            ServerException serverException = (ServerException) ex;
            return ResultWrapper.failure(serverException.getRespMsg());
        }

        return ResultWrapper.failure(Constant.SYS_MSG);
    }

    @ExceptionHandler(ServerException.class)
    public ResultWrapper serverExceptionHandler(Exception ex) {
        System.out.println(ex);
        if (ex instanceof ServerException) {
            ServerException serverException = (ServerException) ex;
            return ResultWrapper.failure(serverException.getRespMsg());
        }

        return ResultWrapper.failure(Constant.SYS_MSG);
    }
}
